<template>
  <div>
    <div class="container" id="container">
      <div class="form-container sign-up-container">
        <div class="overlay-close-btn" @click="handleClose" v-if="isShowBtn">
          <i class="el-dialog__close el-icon el-icon-close"></i>
        </div>
        <el-form style="padding: 0" :model="registerForm" :rules="registerRules" ref="registerForm">
          <h1>Create Account</h1>
          <el-form-item prop="username" style="max-width: 284px;width: 100%">
            <el-input placeholder="Email(登陆账号)" v-model="registerForm.username"></el-input>
          </el-form-item>
          <el-form-item prop="nickname" style="max-width: 284px;width: 100%">
            <el-input placeholder="Name" v-model="registerForm.nickname"></el-input>
          </el-form-item>
          <el-form-item prop="password" style="max-width: 284px;width: 100%">
            <el-input type="password" placeholder="Password" v-model="registerForm.password"
                      @keyup.enter.native="handleRegister('registerForm')"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="danger" style="width: 120px;padding: 12px;" @click="handleRegister('registerForm')">注册
            </el-button>
            <el-button type="danger"
                       style="width: 160px;
                       padding: 12px;
                       border: 0;
                       background-color: darkgrey;"
                       @click="handleReacquire"
                       v-show="isShowReacquire"
                       :disabled="isReacquire">
              <span v-show="isReacquire">{{ reacquireTime }}秒后</span>
              <span>重新发送邮箱激活</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="form-container sign-in-container">
        <el-form :model="loginForm" :rules="loginRules" ref="loginForm">
          <h1>Sign in</h1>
          <div class="social-container">
            <a @click="toGitee()" class="social">
              <svg-icon name="gitee" style="font-size:40px;"></svg-icon>
            </a>
          </div>
          <span style="margin-bottom: 7px;font-size: 14px;">or use your account</span>
          <el-form-item prop="username" style="max-width: 284px;width: 100%">
            <el-input placeholder="Email" v-model="loginForm.username"></el-input>
          </el-form-item>
          <el-form-item prop="password" style="max-width: 284px;width: 100%">
            <el-input type="Password" placeholder="password" v-model="loginForm.password"
                      @keyup.enter.native="handleLogin('loginForm')"></el-input>
          </el-form-item>
          <el-button type="danger" @click="handleLogin('loginForm')">登陆</el-button>
        </el-form>
      </div>
      <div class="overlay-container">
        <div class="overlay-close-btn" @click="$emit('closeDialog')" v-if="!isShowBtn">
          <i class="el-dialog__close el-icon el-icon-close"></i>
        </div>
        <div class="overlay">
          <div class="overlay-panel overlay-left">
            <h1>Welcome Back!</h1>
            <p>已有账号！？点击下方登陆按钮进行登陆</p>
            <button class="ghost" id="signIn" @click="handleSign('in')">登陆</button>
          </div>
          <div class="overlay-panel overlay-right">
            <h1>Hello, Friend!</h1>
            <p>没有账号？点击下注册按钮进行注册</p>
            <button class="ghost" id="signUp" @click="handleSign('up')">注册</button>
          </div>
        </div>
      </div>
    </div>
  </div>

</template>

<script>
import {checkName, register, reacquireEmail, getGiteeUrl} from "@/api/user";
import {initWebsocket} from "@/api/websocket";

export default {
  name: "login",
  data() {
    let checkUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('邮箱不能为空'));
      } else {
        const mailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/
        setTimeout(() => {
          if (mailReg.test(value)) {
            checkName(value).then(res => {
              if (res.status && !res.data.isExist) {
                this.isShowReacquire = true;
                return callback(new Error('邮箱已存在'));
              } else {
                this.isShowReacquire = false;
              }
              callback();
            })
          } else {
            callback(new Error('请输入正确的邮箱格式'))
          }
        }, 100)
      }
    };
    return {
      isReacquire: false,
      isShowReacquire: false,
      reacquireTime: 60,
      isShowBtn: false,
      loginForm: {},
      registerForm: {},
      loginRules: {
        username: [
          {required: true, message: '请输入Email', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ]
      },
      registerRules: {
        username: [
          {validator: checkUsername, trigger: 'blur'},
        ],
        nickname: [
          {required: true, message: '请输入名字', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
      },
    }
  },
  methods: {
    handleReacquire() {
      const username = this.registerForm.username;
      if (username) {
        const mailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/
        if (mailReg.test(username)) {
          checkName(username).then(res => {
            if (res.status && !res.data.isExist) {
              //用户存在才允许重新发送
              reacquireEmail(username).then(res => {
                if (res.code === 'A0204') {
                  this.reacquireTime = res.data;
                  this.$message.error(res.msg);
                } else {
                  this.$message.success(res.msg);
                }
                this.handleCountDown()
              });
            } else {
              this.$message.error('邮箱不存在，请重新注册')
            }
          })
        }
      }
    },
    handleCountDown() {
      this.isReacquire = true
      let timer = setInterval(() => {
        this.reacquireTime--;
        if (this.reacquireTime < 1) {
          this.isReacquire = false
          this.reacquireTime = 60
          clearInterval(timer)
        }
      }, 1000)
    },
    handleClose() {
      this.$emit('closeDialog')
      this.registerForm = {}
      this.loginForm = {}
      this.isShowReacquire = false
    },
    handleSign(type) {
      const container = document.getElementById('container');
      this.isShowBtn = !this.isShowBtn
      if (type == 'in') {
        container.classList.remove("right-panel-active");
      } else {
        container.classList.add("right-panel-active");
      }
    },
    handleLogin(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$store.dispatch('goLogin', {data: this.loginForm, type: 'system'}).then((res) => {
            const {code, msg, status} = res
            console.log(msg);
            console.log(status);
            if (status) {
              this.$store.dispatch('getUserInfo').then(res => {
                initWebsocket(res.id)
              });
              this.$message.success("登陆成功");
              this.handleClose()
            } else {
              this.$message.error(msg);
            }
          })
        } else {
          return false;
        }
      });
    },
    handleRegister(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.isShowReacquire = true
          register(this.registerForm).then(res => {
            if (res.status) {
              this.$message.success('注册成功!!!记得去邮箱激活再登陆噢~~')
            } else {
              this.$message.error('注册失败，请稍后再试...')
            }
          })
        } else {
          return false;
        }
      });
    },
    toGitee() {
      getGiteeUrl().then(res => {
        //打开新的登陆窗口
        let windowObj = window.open(res,
            '_blank',
            'height=600, width=800, left=250, top=150, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no')
        //监听窗口关闭
        let loop = setInterval(function () {
          if (windowObj != null && windowObj.closed) {
            clearInterval(loop);
            //刷新当前页面
            location.reload()
          }
        }, 800);
      })
    }
  },
}
</script>
<style scoped>

* {
  box-sizing: border-box;
}

body {
  background: #f6f5f7;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  font-family: 'Montserrat', sans-serif;
  height: 100vh;
  margin: -20px 0 50px;
}

h1 {
  font-weight: bold;
  margin: 0;
  display: block;
  font-size: 2em;
  margin-block-start: 0.67em;
  margin-block-end: 0.67em;
  margin-inline-start: 0px;
  margin-inline-end: 0px;
  font-weight: bold;
}

h2 {
  text-align: center;
}

p {
  font-size: 14px;
  font-weight: 100;
  line-height: 20px;
  letter-spacing: 0.5px;
  margin: 20px 0 30px;
}

span {
  font-size: 12px;
}

a {
  color: #333;
  font-size: 14px;
  text-decoration: none;
  margin: 15px 0;
}

button {
  border-radius: 20px;
  border: 1px solid #FF4B2B;
  background-color: #FF4B2B;
  color: #FFFFFF;
  font-size: 12px;
  font-weight: bold;
  padding: 12px 45px;
  letter-spacing: 1px;
  text-transform: uppercase;
  transition: transform 80ms ease-in;
}

button:active {
  transform: scale(0.95);
}

button:focus {
  outline: none;
  background-color: #FF4B2B;
}

button:hover {
  background-color: #FF4B2B;
}

button.ghost {
  background-color: transparent;
  border-color: #FFFFFF;
}

form {
  background-color: #FFFFFF;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: 0 50px;
  height: 100%;
  text-align: center;
}

input {
  background-color: #eee;
  border: none;
  padding: 12px 15px;
  margin: 8px 0;
  width: 100%;
}

.container {
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25),
  0 10px 10px rgba(0, 0, 0, 0.22);
  position: relative;
  overflow: hidden;
  width: 768px;
  max-width: 100%;
  min-height: 480px;
  margin: 0 auto;
}

.form-container {
  position: absolute;
  top: 0;
  height: 100%;
  transition: all 0.6s ease-in-out;
}

.sign-in-container {
  left: 0;
  width: 50%;
  z-index: 2;
}

.container.right-panel-active .sign-in-container {
  transform: translateX(100%);
}

.sign-up-container {
  left: 0;
  width: 50%;
  opacity: 0;
  z-index: 1;
}

.container.right-panel-active .sign-up-container {
  transform: translateX(100%);
  opacity: 1;
  z-index: 5;
  animation: show 0.6s;
}

@keyframes show {
  0%, 49.99% {
    opacity: 0;
    z-index: 1;
  }

  50%, 100% {
    opacity: 1;
    z-index: 5;
  }
}

.overlay-container {
  position: absolute;
  top: 0;
  left: 50%;
  width: 50%;
  height: 100%;
  overflow: hidden;
  transition: transform 0.6s ease-in-out;
  z-index: 100;
}

.container.right-panel-active .overlay-container {
  transform: translateX(-100%);
}

.overlay {
  background: #FF416C;
  background: -webkit-linear-gradient(to right, #FF4B2B, #FF416C);
  background: linear-gradient(to right, #FF4B2B, #FF416C);
  background-repeat: no-repeat;
  background-size: cover;
  background-position: 0 0;
  color: #FFFFFF;
  position: relative;
  left: -100%;
  height: 100%;
  width: 200%;
  transform: translateX(0);
  transition: transform 0.6s ease-in-out;
}

.container.right-panel-active .overlay {
  transform: translateX(50%);
}

.overlay-panel {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: 0 40px;
  text-align: center;
  top: 0;
  height: 100%;
  width: 50%;
  transform: translateX(0);
  transition: transform 0.6s ease-in-out;
}

.overlay-left {
  transform: translateX(-20%);
}

.container.right-panel-active .overlay-left {
  transform: translateX(0);
}

.overlay-right {
  right: 0;
  transform: translateX(0);
}

.container.right-panel-active .overlay-right {
  transform: translateX(20%);
}

.social-container {
  margin: -7px 0 10px 0;
}

.social-container a {
  border: 1px solid #DDDDDD;
  border-radius: 50%;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  margin: 0 5px;
  height: 40px;
  width: 40px;
}

.overlay-close-btn {
  position: absolute;
  z-index: 10;
  top: 2px;
  cursor: pointer;
  right: 8px;
  font-size: 20px;
}
</style>
